package com.casic.worklog.jwt;

import com.alibaba.fastjson.JSONObject;
import com.casic.worklog.model.Employee;
import com.casic.worklog.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api")
public class JwtLogin {
    @Autowired
    EmployeeService userService;

    //登录
    @PostMapping("/login")
    public Object login(Employee user) {
        JSONObject jsonObject = new JSONObject();
        Employee userForBase =userService.findByUsername(user.getUsername());
        if (userForBase == null) {
            jsonObject.put("message" , "登录失败,用户不存在");
            return jsonObject;
        } else {
            if (!userForBase.getPassword().equals(user.getPassword())) {
                jsonObject.put("message" , "登录失败,密码错误");
                return jsonObject;
            } else {
                String token = JwtUtil.sign(userForBase.getUsername(),userForBase.getId()+"",36000000);
                jsonObject.put("token" , token);
                jsonObject.put("user" , userForBase);
                return jsonObject;
            }
        }
    }

    @UserLoginToken
    @GetMapping("/getMessage")
    public String getMessage() {
        return "你已通过验证";
    }
}
